package com.ruoyi.project.manage.project.controller;

import com.ruoyi.common.constant.ProjectConstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.manage.item.domain.ProjectItem;
import com.ruoyi.project.manage.item.domain.StageItem;
import com.ruoyi.project.manage.item.service.IProjectItemService;
import com.ruoyi.project.manage.item.service.IStageItemService;
import com.ruoyi.project.manage.project.domain.Project;
import com.ruoyi.project.manage.project.service.IProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目事项Controller
 * 
 * @author fangkun
 * @date 2022-03-28
 */
@Controller
@RequestMapping("/manage/items")
public class ProjectItemController extends BaseController
{
    private String prefix = "manage/project";

    @Autowired
    private IProjectService projectService;
    @Autowired
    private IProjectItemService projectItemService;
    @Autowired
    private IStageItemService stageItemService;

    @RequiresPermissions("manage:items:view")
    @GetMapping()
    public String project()
    {
        return prefix + "/projectItems";
    }

    /**
     * 查询项目信息列表
     */
    @RequiresPermissions("manage:items:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Project project)
    {
        startPage();
        project.setProjectStatus(ProjectConstants.APPROVAL_AGREE);
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 修改项目信息
     */
    @RequiresPermissions("manage:items:edit")
    @GetMapping("/edit/{projectId}")
    public String edit(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        // 获取项目信息
        Project project = projectService.selectProjectByProjectId(projectId);
        mmap.put("project", project);

        // 获取项目关联事项
        ProjectItem projectItem = new ProjectItem();
        projectItem.setProjectId(projectId);
        projectItem.setStatus(UserConstants.NORMAL);
        List<ProjectItem> projectItems = projectItemService.selectProjectItemList(projectItem);

        String s = projectItems.stream().map(p->p.getItemId().intValue() + "").collect(Collectors.joining(","));
        mmap.put("projectItems", ","+s+",");

        // 获取项目关联事项列表
        List<StageItem> stageItems = stageItemService.selectStageItemList(new StageItem());
        mmap.put("stageItems", stageItems);
        return prefix + "/editItems";
    }

    /**
     * 修改保存项目事项
     */
    @RequiresPermissions("manage:item:edit")
    @Log(title = "项目事项", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProjectItem projectItem)
    {
        return toAjax(projectItemService.updateProjectItem(projectItem));
    }
}
